package com.kangfu.api.exception;

import com.kangfu.common.constant.HttpStatusConstant;
import com.kangfu.common.exception.AuthorizationException;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.model.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.security.PublicKey;
import java.util.Set;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理token校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public Object authorizationExceptionHadler(AuthorizationException e) {
        return AjaxResult.error(HttpStatusConstant.UNAUTHORIZED, e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Object BusinessExceptionHadler(BusinessException e) {
        System.out.println("-=================业务异常 start ");
        System.out.println(e.getMessage());
        System.out.println("-=================业务异常 end ");
        return AjaxResult.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        String errMsg = e.getBindingResult().getFieldError().getDefaultMessage();
        System.out.println("-=================参数校验失败 start ");
        System.out.println(errMsg);
        System.out.println("-=================参数校验失败 end ");
        return AjaxResult.error(errMsg);
    }


    @ExceptionHandler(ConstraintViolationException.class)
    public Object MissingServletRequestParameterExceptionHandler(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        String errMsg = constraintViolations.iterator().next().getMessage();
        System.out.println("-=================参数校验失败 start ");
        System.out.println(errMsg);
        System.out.println("-=================参数校验失败 end ");
        return AjaxResult.error(errMsg);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public AjaxResult MissingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        String errMsg = e.getParameterName() + " is required";
        return AjaxResult.error(errMsg);
    }

    /**
     * 处理所有非自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult exceptionHadler(Exception e){
        System.out.println("-=================非自定义异常 start ");
        System.out.println(e.getMessage());
        log.error("系统异常:  " ,e);
        System.out.println("-=================非自定义异常 end ");

        return  AjaxResult.error("服务端异常");
    }
}
